#include <QListWidget>
#include <QCheckBox>
#include <QList>
#include <QLineEdit>
#include <QDebug>

#include "llCombobox2.h"

class LLComboBox2Private
{
public:

    QStringList selectedDataList;
    QListWidget *pListWidget;
};

LLComboBox2::LLComboBox2(QWidget *parent)
    : QComboBox(parent)
    , m_pd(new LLComboBox2Private)
{
    m_pd->pListWidget = new QListWidget;
    m_pd->pListWidget->setFixedHeight(200);
#ifdef Q_OS_ANDROID
    m_pd->pListWidget->setStyleSheet("QListWidget::indicator{height:15px; width:15px;}");
    m_pd->pListWidget->setSpacing(5);
#endif

    QPalette palette = m_pd->pListWidget->palette();
    this->setEditable(false);
    connect(m_pd->pListWidget, SIGNAL(itemChanged(QListWidgetItem *)), this, SLOT(slot_itemChanged(QListWidgetItem *)));
    setView(m_pd->pListWidget);
    //设置Editable为true，lineEdit() 函数才不返回空
    setEditable(true);
    lineEdit()->setReadOnly(true);
    addDataItem("全部");
    this->setFixedHeight(40);
}

void LLComboBox2::init(){
}

void LLComboBox2::addDataItem(const QString &text, const QVariant &userData)
{
    m_pd->pListWidget->blockSignals(true);
    QListWidgetItem *pItem = new QListWidgetItem(text, m_pd->pListWidget);
    pItem->setData(Qt::UserRole, userData);
    pItem->setCheckState(Qt::Checked);
    m_pd->selectedDataList.append(pItem->text());

    //设置QListWidgetItem 可交互且可以选中和取消选中
    pItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    QComboBox::addItem(text);
    QComboBox::setCurrentText("");
    m_pd->pListWidget->blockSignals(false);

    QString tt = "";
    if(m_pd->selectedDataList.contains("全部")){
        lineEdit()->setText("全部");
        return;
    }

    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        tt.append(m_pd->selectedDataList[var]);
        if(var < m_pd->selectedDataList.size() - 1)
        {
            tt.append(",");
        }
    }

    lineEdit()->setText(tt);
}

void LLComboBox2::setSelectedData(const QStringList &selectedData)
{
    m_pd->selectedDataList.clear();
    m_pd->selectedDataList += selectedData;

    QString text = "";
    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        m_pd->pListWidget->blockSignals(true);
        QListWidgetItem *pItem = m_pd->pListWidget->item(var);
        pItem->setCheckState(Qt::Checked);
        m_pd->pListWidget->blockSignals(false);

        text.append(m_pd->selectedDataList[var]);
        if(var < m_pd->selectedDataList.size() - 1)
        {
            text.append(",");
        }
    }

    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        text.append(m_pd->selectedDataList[var]);
        if(var < m_pd->selectedDataList.size() - 1)
        {
            text.append(",");
        }
    }

    lineEdit()->setText(text);
}

QStringList LLComboBox2::selectedDataText()
{
    return m_pd->selectedDataList;
}

QList<QVariant> LLComboBox2::selectedUserData()
{
    QList<QVariant> dataList;
    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        QListWidgetItem *pItem = m_pd->pListWidget->item(var);
        if(pItem->checkState() == Qt::Checked)
        {
            dataList.append(pItem->data(Qt::UserRole));
        }
    }

    return dataList;
}

void LLComboBox2::slot_itemChanged(QListWidgetItem *item)
{
    if(m_pd->selectedDataList.contains(item->text()))
    {
        if(item->text()=="全部"){     // 取消全选
            m_pd->selectedDataList.clear();

            for(int i = 0; i < m_pd->pListWidget->count(); i ++){
                QListWidgetItem *pItem = m_pd->pListWidget->item(i);

                m_pd->pListWidget->blockSignals(true);
                pItem->setCheckState(Qt::Unchecked);

                m_pd->pListWidget->blockSignals(false);
            }
        }else{

            if(m_pd->selectedDataList.contains("全部")){
                QListWidgetItem *pItem = m_pd->pListWidget->item(0);
                m_pd->pListWidget->blockSignals(true);
                pItem->setCheckState(Qt::Unchecked);    // 取消全选
                m_pd->pListWidget->blockSignals(false);
                m_pd->selectedDataList.removeOne("全部");
            }


            m_pd->selectedDataList.removeOne(item->text());
        }

    }
    else
    {
        if(item->text()=="全部"){     // 全选
            m_pd->selectedDataList.clear();
            for(int i = 0; i < m_pd->pListWidget->count(); i ++){
                QListWidgetItem *pItem = m_pd->pListWidget->item(i);

                m_pd->pListWidget->blockSignals(true);
                pItem->setCheckState(Qt::Checked);

                m_pd->pListWidget->blockSignals(false);
                m_pd->selectedDataList.append(pItem->text());
            }

        }else{
            m_pd->selectedDataList.append(item->text());
        }
    }



    QString text = "";
    if(m_pd->selectedDataList.contains("全部")){
        lineEdit()->setText("全部");
        return;
    }

    for (int var = 0; var < m_pd->selectedDataList.size(); ++var)
    {
        text.append(m_pd->selectedDataList[var]);
        if(var < m_pd->selectedDataList.size() - 1)
        {
            text.append(",");
        }
    }

    lineEdit()->setText(text);

}

void LLComboBox2::DataClear(){
    m_pd->pListWidget->blockSignals(true);
    m_pd->pListWidget->clear();
    m_pd->pListWidget->blockSignals(false);
}

void LLComboBox2::wheelEvent(QWheelEvent *e){
    // 禁用滚轮
}

